<?php
/***************************************************************************
 *   copyright : (C) 2009 Udrea Cristian
 *   site : http://code.google.com/p/testauction-php
 ***************************************************************************/

/***************************************************************************
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as 
 *   published by the Free Software Foundation; either version 2 of the
 *   License, or (at your option) any later version. Although none of the
 *   code may be sold. If you have been sold this script, get a refund.
 ***************************************************************************/
include("include/session.php");
include("include/classes/class.AuctionHandler.php");

function getDateItems($inDate) {
	return "new Date(".date('Y,n,j,G,',$inDate).intval(date('i',$inDate)).','.intval(date('s',$inDate)).")";
}

header('Content-Type: application/xml');
echo "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n";
?>
<response>
	<status><?php $hour=date('H', time()); echo ($hour > OPENING_HOUR && $hour < CLOSING_HOUR) ? "false" : "true";?></status>
	<today><?php echo getDateItems(time()); ?></today>
<?php
$result = array();
$lastBidders = array();
$count = 0;
if(isset($_GET['id']) && isset($_GET['type']) && $_GET['type'] == '2') {
	$result = array( 0 => $auctionDB->getAuctionData($_GET['id']) );
	$lastBidders = $auctionDB->getAuctionLastOffers($_GET['id']);
	if(count($result) > 0)$count = 1;
} else {
	$result = $auctionDB->getOpenAuctions($_GET['id']);
	$count = count($result);
}

for($i=0; $i < $count; $i++) {
?>
	<bidItem>
		<bidId><?php echo $result[$i]['auction_id']; ?></bidId>
		<timeToGo><?php echo $result[$i]['time_left']; ?></timeToGo>
		<currentPrice><?php echo $auctionDB->getAuctionOfferCount($result[$i]['auction_id'])*$result[$i]['offer_increment']+$result[$i]['base_price']; ?></currentPrice>
		<lastBid><?php $latestoffer = $auctionDB->getAuctionLatestOffer($result[$i]['auction_id']);	echo $latestoffer['username'];?></lastBid>
		<isOpen><?php echo ($result[$i]['closed']) ? "false" : "true"; ?></isOpen>
		<tDate><?php if($result[$i]['closed']) {echo getDateItems($result[$i]['closed_timestamp']);} else {echo getDateItems($result[$i]['starts_timestamp']);}?></tDate>
	    <?php if($_GET['type'] == '2') { ?>
		<lastTen>
		<?php foreach($lastBidders as $item) { ?>
			<onePerson>
				<username><?php echo $item['username']; ?></username>
				<bidTime><?php echo $item['date']; ?></bidTime>
				<bidPrice><?php echo $item['price']; ?></bidPrice>
			</onePerson>
		<?php } ?>
		</lastTen>
		<?php } ?>
	</bidItem>
<?php } ?>
</response>
